<template>
  <c-dialog ref="dialog" :data="selData" single title="审核图纸" @open="openHandle">
    <c-list ref="form" :model="formData" :rules="formRules" label-width="80px">
      <c-list-item label="审核结果" prop="approvalResult">
        <el-select v-model="formData.approvalResult" clearable filterable @change="v=>$set(formRules.approvalAdvice[0], 'required', v === '0')">
          <el-option value="1" label="同意" />
          <el-option value="0" label="不同意" />
        </el-select>
      </c-list-item>
      <c-list-item label="审核意见" prop="approvalAdvice">
        <el-input v-model="formData.approvalAdvice" type="textarea" maxlength="1024" show-word-limit :autosize="{minRows:3}" placeholder="审核意见" clearable />
      </c-list-item>
      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button key="confirm" type="primary" @click="confirmHandle">确认</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { putPictureApproval } from '@/api/shopDrawing'
export default {
  name: 'AuditDraw',
  components: {
  },
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    const validateAA = (r, v, c) => {
      if (this.formData.approvalResult === '0' && this.formData.approvalAdvice === '') {
        c(new Error('请输入审核意见'))
      } else {
        c()
      }
    }
    return {
      formData: {
        approvalResult: '',
        approvalAdvice: ''
      },
      formRules: {
        approvalResult: [{ required: true, message: '请选择审核结果', trigger: 'change' }],
        approvalAdvice: [{ validator: validateAA, trigger: 'change' }]
      }
    }
  },
  computed: {
    detail() {
      return this.selData.length === 0 ? {} : this.selData[0]
    }
  },
  methods: {
    openHandle() {
    },
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        const data = {}
        data.approvalResult = this.formData.approvalResult
        data.approvalAdvice = this.formData.approvalAdvice
        data.pictureMaintainId = this.detail.id
        data.id = this.detail.approverId
        return putPictureApproval(data).then(res => {
          this.$message.success('操作成功！')
          this.$refs.dialog.visible = false
          this.$emit('on-success')
        })
      })
    },
    cancelHandle() {
      return this.$msgbox({
        message: '是否取消当前操作？',
        title: '取消',
        showCancelButton: true,
        type: 'warning'
      }).then(() => {
        this.$refs.dialog.visible = false
      })
    }
  }
}
</script>
